/*
 * Copyright (c) 2017-present, CV4J Contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.core.filters;

import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.image.util.Tools;

import java.util.Optional;

/**
 * 反对bri过滤器
 * this filter illustrate the brightness and contrast of the image
 * and demo how to change the both attributes of the image.
 *
 * @author dev
 * @date 2021/07/22
 */
public class ConBriFilter extends BaseFilter {
    private float contrast = 1.2f; // default value;
    private float brightness = 0.7f; // default value;

    /**
     * 做的过滤器
     *
     * @param src src
     * @return {@link Optional<ImageProcessor>}
     */
    @Override
    public Optional<ImageProcessor> doFilter(ImageProcessor src) {
        // calculate RED, GREEN, BLUE means of pixel
        int[] rgbmeans = new int[3];
        double redSum = 0;
        double greenSum = 0;
        double blueSum = 0;
        int total = height * width;
        int r1 = 0;
        int g1 = 0;
        int b1 = 0;
        for (int i = 0; i < total; i++) {
            r1 = RED[i] & 0xff;
            g1 = GREED[i] & 0xff;
            b1 = BLUE[i] & 0xff;
            redSum += r1;
            greenSum += g1;
            blueSum += b1;
        }
        rgbmeans[0] = (int) (redSum / total);
        rgbmeans[1] = (int) (greenSum / total);
        rgbmeans[2] = (int) (blueSum / total);

        // adjust contrast and brightness algorithm, here
        for (int i = 0; i < total; i++) {
            r1 = RED[i] & 0xff;
            g1 = GREED[i] & 0xff;
            b1 = BLUE[i] & 0xff;

            // remove means
            r1 -= rgbmeans[0];
            g1 -= rgbmeans[1];
            b1 -= rgbmeans[2];

            // adjust contrast now !!!
            r1 = (int) (r1 * getContrast());
            g1 = (int) (g1 * getContrast());
            b1 = (int) (b1 * getContrast());

            // adjust brightness
            r1 += (int) (rgbmeans[0] * getBrightness());
            g1 += (int) (rgbmeans[1] * getBrightness());
            b1 += (int) (rgbmeans[2] * getBrightness());

            RED[i] = (byte) Tools.clamp(r1);
            GREED[i] = (byte) Tools.clamp(g1);
            BLUE[i] = (byte) Tools.clamp(b1);
        }
        return Optional.ofNullable(src);
    }

    /**
     * 得到的对比
     *
     * @return float
     */
    public float getContrast() {
        return contrast;
    }

    /**
     * 组对比
     *
     * @param contrast 对比
     */
    public void setContrast(float contrast) {
        this.contrast = contrast;
    }

    /**
     * 得到亮度
     *
     * @return float
     */
    public float getBrightness() {
        return brightness;
    }

    /**
     * 设置亮度
     *
     * @param brightness 亮度
     */
    public void setBrightness(float brightness) {
        this.brightness = brightness;
    }

}
